import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';

import { $t } from '#/locales';

/**
 * 查询参数
 */
export function querySchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'title',
      label: $t('system.title'),
    },
    {
      component: 'Input',
      fieldName: 'subTitle',
      label: $t('system.subTitle'),
    },
    {
      component: 'Input',
      fieldName: 'operName',
      label: $t('system.operate') + $t('user.name'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        clearable: true,
        options: [
          {
            label: $t('system.type.other'),
            value: '0',
          },
          {
            label: $t('system.type.insert'),
            value: '1',
          },
          {
            label: $t('system.type.update'),
            value: '2',
          },
          {
            label: $t('system.type.delete'),
            value: '3',
          },
          {
            label: $t('system.type.grant'),
            value: '4',
          },
          {
            label: $t('system.type.export'),
            value: '5',
          },
          {
            label: $t('system.type.import'),
            value: '6',
          },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'operType',
      label: $t('system.operate') + $t('common.type'),
    },
    {
      component: 'DatePicker',
      componentProps: {
        rangeSeparator: '-',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'daterange',
      },
      fieldName: 'dateRange',
      label: $t('common.createTime'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          {
            label: $t('common.success'),
            value: '0',
          },
          {
            label: $t('common.fail'),
            value: '1',
          },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function columnsSchema<T>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions<T>['columns'] {
  return [
    { type: 'checkbox', width: 50 },
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'title',
      title: $t('system.title'),
    },
    {
      field: 'subTitle',
      title: $t('system.subTitle'),
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          {
            label: $t('system.type.other'),
            value: '0',
          },
          {
            label: $t('system.type.insert'),
            value: '1',
          },
          {
            label: $t('system.type.update'),
            value: '2',
          },
          {
            label: $t('system.type.delete'),
            value: '3',
          },
          {
            label: $t('system.type.grant'),
            value: '4',
          },
          {
            label: $t('system.type.export'),
            value: '5',
          },
          {
            label: $t('system.type.import'),
            value: '6',
          },
        ],
      },
      field: 'operType',
      title: $t('system.operate') + $t('common.type'),
    },
    {
      cellRender: {
        name: 'CellTag',
      },
      field: 'requestMethod',
      title: $t('system.requestMethod'),
    },
    {
      field: 'operName',
      title: $t('system.operate') + $t('user.name'),
    },
    {
      field: 'operIp',
      title: 'ip',
    },
    {
      field: 'operLocation',
      title: $t('common.address'),
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          {
            label: $t('common.success'),
            value: '0',
          },
          {
            label: $t('common.fail'),
            value: '1',
            type: 'danger',
          },
        ],
      },
      field: 'status',
      title: $t('common.status'),
    },
    {
      field: 'operTime',
      title: $t('common.createTime'),
    },
    {
      cellRender: {
        attrs: {
          nameField: 'title',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'query',
            label: '详情',
            text: true,
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      align: 'center',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('page.action.action'),
    },
  ];
}
